﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DGBY.BLL;
using DGBY.Model;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SelfCompMng : DockContent
    {
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private DGBY.BLL.BYCompanyTab bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BYCompBankCountTab bllBYCompBankCountTab = new DGBY.BLL.BYCompBankCountTab();
        private DataSet m_ds = new DataSet();
        private DataSet m_AccountDs = new DataSet();
        int m_nSelectCompID = 0;
        int m_nSelectCompAccountID = 0;

        public Frm_SelfCompMng()
        {
            InitializeComponent();
        }



        private void ShowAllRecordInDataGridView()
        {
            dataGridViewCompList.DataSource = null;
            m_ds = bllBYCompanyTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompList.DataSource = m_ds.Tables[0];
                dataGridViewCompList.Columns[0].Visible = false;
            }

        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewCompList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBYCompID = this.dataGridViewCompList.Rows[this.dataGridViewCompList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectCompID = Convert.ToInt32(strBYCompID, 10);
            cellclick();
        }

        private void cellclick()
        {
            DGBY.Model.BYCompanyTab modelBYCompanyTab = bllBYCompanyTab.GetModel(m_nSelectCompID);
            if (modelBYCompanyTab != null)
            {
                this.textBoxCompName.Text = modelBYCompanyTab.CompanyName;
                this.textBoxEnglishName.Text = modelBYCompanyTab.YWCompanyName;
                this.textBoxCompAbbr.Text = modelBYCompanyTab.CompanyNameJX;
                this.textBoxAddress.Text = modelBYCompanyTab.Address;
                this.textBoxPhone.Text = modelBYCompanyTab.Telephone;
                this.textBoxFax.Text = modelBYCompanyTab.Fax;
                this.textBoxManagerName.Text = modelBYCompanyTab.ManagerName;
                this.textBoxTaxPayerIndentityCode.Text = modelBYCompanyTab.CountPeoCode;
                this.richTextBoxNote.Text = modelBYCompanyTab.Note;
            }
            else
            {
                this.textBoxCompName.Text = "";
                this.textBoxEnglishName.Text = "";
                this.textBoxCompAbbr.Text = "";
                this.textBoxAddress.Text = "";
                this.textBoxPhone.Text = "";
                this.textBoxFax.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxTaxPayerIndentityCode.Text = "";
                this.richTextBoxNote.Text = "";
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BYCompanyTab modelBYCompanyTab = new DGBY.Model.BYCompanyTab();
                modelBYCompanyTab.CompanyName = this.textBoxCompName.Text;
                modelBYCompanyTab.YWCompanyName = this.textBoxEnglishName.Text;
                modelBYCompanyTab.CompanyNameJX = this.textBoxCompAbbr.Text;
                modelBYCompanyTab.Address = this.textBoxAddress.Text;
                modelBYCompanyTab.Telephone = this.textBoxPhone.Text;
                modelBYCompanyTab.Fax = this.textBoxFax.Text;
                modelBYCompanyTab.ManagerName = this.textBoxManagerName.Text;
                modelBYCompanyTab.CountPeoCode = this.textBoxTaxPayerIndentityCode.Text;
                modelBYCompanyTab.Note = this.richTextBoxNote.Text;

                int nResult = bllBYCompanyTab.Add(modelBYCompanyTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelBYCompanyTab.CompanyName);

                    m_nSelectCompID = bllBYCompanyTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                InitSelfCompCombox();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }
        //有bug，当新增一条记录后，继续修改，报错
        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (bllBYCompanyTab.GetModel(m_nSelectCompID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.BYCompanyTab modelBYCompanyTab = bllBYCompanyTab.GetModel(m_nSelectCompID);
                //modelBYCompanyTab.BYCompanyTabID = m_nSelectCompID;
                modelBYCompanyTab.CompanyName = this.textBoxCompName.Text;
                modelBYCompanyTab.YWCompanyName = this.textBoxEnglishName.Text;
                modelBYCompanyTab.CompanyNameJX = this.textBoxCompAbbr.Text;
                modelBYCompanyTab.Address = this.textBoxAddress.Text;
                modelBYCompanyTab.Telephone = this.textBoxPhone.Text;
                modelBYCompanyTab.Fax = this.textBoxFax.Text;
                modelBYCompanyTab.ManagerName = this.textBoxManagerName.Text;
                modelBYCompanyTab.CountPeoCode = this.textBoxTaxPayerIndentityCode.Text;
                modelBYCompanyTab.Note = this.richTextBoxNote.Text;

                bllBYCompanyTab.Update(modelBYCompanyTab);

                Frm_Main.Log2DB(this.Text, "修改", modelBYCompanyTab.CompanyName);
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (bllBYCompanyTab.GetModel(m_nSelectCompID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            bllBYCompanyTab.Delete(m_nSelectCompID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclick();
            InitSelfCompCombox();
            ShowAllAccountRecordInDataGridView();
        }

        private void dataGridViewCompList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void InitSelfCompCombox()
        {
            DataSet ds = bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxCompName.DataSource = ds.Tables[0];
                this.comboBoxCompName.DisplayMember = "经营单位名称";
                this.comboBoxCompName.ValueMember = "BYCompanyTabID";
                this.comboBoxCompName.SelectedIndex = 0;
            }
        }

        private void Frm_SelfCompMng_Load(object sender, EventArgs e)
        {
            InitSelfCompCombox();
        }

        private void ShowAllAccountRecordInDataGridView()
        {
            dataGridViewCompBankList.DataSource = null;
            m_AccountDs = bllBYCompBankCountTab.GetAllAccountInfo();
            if (m_AccountDs.Tables.Count > 0 && m_AccountDs.Tables[0].Rows.Count > 0)
            {
                dataGridViewCompBankList.DataSource = m_AccountDs.Tables[0];
                dataGridViewCompBankList.Columns[0].Visible = false;

            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelete.Visible = true;
                this.buttonBankDel.Visible = true;
            }
        }

        private void buttonBankShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllAccountRecordInDataGridView();
        }

        private void dataGridViewCompBankList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCompBankList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonBankAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = bllBYCompBankCountTab.GetList("CountNo = '" + this.textBoxAccount.Text + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("银行账号已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.BYCompBankCountTab modelBYCompBankCountTab = new DGBY.Model.BYCompBankCountTab();
                modelBYCompBankCountTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxCompName.SelectedValue.ToString(), 10);
                modelBYCompBankCountTab.CountName = this.textBoxAccountName.Text;
                modelBYCompBankCountTab.KHBank = this.textBoxBank.Text;
                modelBYCompBankCountTab.CountNo = this.textBoxAccount.Text;

                int nResult = bllBYCompBankCountTab.Add(modelBYCompBankCountTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加开户行及帐号", this.comboBoxCompName.Text + "(" + modelBYCompBankCountTab.CountNo  + ")");

                    m_nSelectCompAccountID = bllBYCompBankCountTab.GetMaxId() - 1;
                    ShowAllAccountRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
  
        }

        private void buttonBankDel_Click(object sender, EventArgs e)
        {
            if (bllBYCompBankCountTab.GetModel(m_nSelectCompAccountID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bllBYCompBankCountTab.Delete(m_nSelectCompAccountID);
            ShowAllAccountRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            cellAccountClick();
        }

        private void buttonBankModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (bllBYCompBankCountTab.GetModel(m_nSelectCompAccountID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.BYCompBankCountTab modelBYCompBankCountTab = bllBYCompBankCountTab.GetModel(m_nSelectCompAccountID);
                modelBYCompBankCountTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxCompName.SelectedValue.ToString(), 10);
                modelBYCompBankCountTab.CountName = this.textBoxAccountName.Text;
                modelBYCompBankCountTab.KHBank = this.textBoxBank.Text;
                modelBYCompBankCountTab.CountNo = this.textBoxAccount.Text;
                //modelBYCompBankCountTab.BYCompBankCountTabID = m_nSelectCompAccountID;

                bllBYCompBankCountTab.Update(modelBYCompBankCountTab);

                Frm_Main.Log2DB(this.Text, "修改开户行及帐号", this.comboBoxCompName.Text + "(" + modelBYCompBankCountTab.CountNo + ")");
                ShowAllAccountRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void dataGridViewCompBankList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBYCompIAccountD = this.dataGridViewCompBankList.Rows[this.dataGridViewCompBankList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectCompAccountID = Convert.ToInt32(strBYCompIAccountD, 10);
            cellAccountClick();
        }

        private void cellAccountClick()
        {
            DGBY.Model.BYCompBankCountTab modelBYCompBankCountTab = bllBYCompBankCountTab.GetModel(m_nSelectCompAccountID);
            if (modelBYCompBankCountTab != null)
            {
                this.textBoxAccountName.Text = modelBYCompBankCountTab.CountName;
                this.textBoxBank.Text = modelBYCompBankCountTab.KHBank;
                this.textBoxAccount.Text = modelBYCompBankCountTab.CountNo;
                this.comboBoxCompName.SelectedValue = modelBYCompBankCountTab.BYCompanyTabID;
            }
            else
            {
                this.textBoxAccountName.Text = "";
                this.textBoxBank.Text = "";
                this.textBoxAccount.Text = "";
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewCompList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "SelfCompInfo.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonBankExport_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewCompBankList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "SelfCompBankInfo.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


    }
}